#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
void writer(int wfd)
{
    const char* str = "hello father, I am child";
    char buffer[128];
    int cnt = 0;
    pid_t pid = getpid();
    while(1)
    {
       snprintf(buffer,sizeof(buffer),"message:%s ,pid:%d ,count:%d\n",str,pid,cnt); 
       write(wfd,buffer,strlen(buffer));
       cnt++;
       sleep(1);
    }
    
}

void reader(int rfd)
{
    char buffer[1024];
    while(1)
    {
        size_t n = read(rfd,buffer,sizeof(buffer)-1);
        (void)n;
        printf("father get a message:%s",buffer);
    }
}

int main()
{
    int pipefe[2];
    int n = pipe(pipefe);

    if(n < 0) return 1;

    printf("pipefe[0]:%d,pipefe[1]:%d\n",pipefe[0],pipefe[1]);
    
    pid_t id = fork();
    if(id == 0)
    {
        close(pipefe[0]);
        
        writer(pipefe[1]);

        exit(0);
    }
    
    close(pipefe[1]);
    reader(pipefe[0]);
    wait(NULL);

    return 0;
}






//#include <stdio.h>

//int main()
//{
//    printf("hello world\n");
//    return 0;
//}



